#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "gps.h"
#include "string.h"
#include "lcd.h"
#include "text.h"	
#include "usart3.h"
#include "sim.h"
#include "wifi.h"
#include "exit.h"


extern _SaveData Save_Data;
char TemMsg[100];
int People=0;


	



//ALIENTEK 探索者STM32F407开发板 实验4
//串口通信实验 -库函数版本
//技术支持：www.openedv.com
//淘宝店铺：http://eboard.taobao.com
//广州市星翼电子科技有限公司  
//作者：正点原子 @ALIENTEK



void XFS_FrameInfo(const char *HZdata)
{
	 u8 Frame_Info[45];
	 unsigned  int  HZ_Length;   
	 HZ_Length =strlen(HZdata);
	 Frame_Info[0] = 0xFD ; 			//构造帧头FD
		 Frame_Info[1] = 0x00 ; 			//构造数据区长度的高字节
		 Frame_Info[2] = HZ_Length+2; 		//构造数据区长度的低字节
		 Frame_Info[3] = 0x01 ; 			//构造命令字：合成播放命令		 		 
		 Frame_Info[4] = 0x01 ;       //文本编码格式：GBK 
	memcpy(&Frame_Info[5], HZdata, HZ_Length);
	uart2SendChars(Frame_Info,5+HZ_Length);
}


void parseGpsBuffer()
{
	char *subString;
	char *subStringNext;
	char i = 0;
	if (Save_Data.isGetData)
	{
		Save_Data.isGetData = false;
		//printf("**************\r\n");
		//printf(Save_Data.GPS_Buffer);

		
		for (i = 0 ; i <= 7 ; i++)
		{
			if (i == 0)
			{
				if ((subString = strstr(Save_Data.GPS_Buffer, ",")) == NULL)
					;//errorLog(1);	//解析错误
			}
			else
			{
				subString++;
				if ((subStringNext = strstr(subString, ",")) != NULL)
				{
					char usefullBuffer[2]; 
					switch(i)
					{
						case 1:memcpy(Save_Data.UTCTime, subString, subStringNext - subString);break;	//获取UTC时间
						case 2:memcpy(usefullBuffer, subString, subStringNext - subString);break;	
						case 3:memcpy(Save_Data.latitude, subString, subStringNext - subString);break;	//获取纬度信息
						case 4:memcpy(Save_Data.N_S, subString, subStringNext - subString);break;	//获取N/S
						case 5:memcpy(Save_Data.longitude, subString, subStringNext - subString);break;	//获取经度信息
						case 6:memcpy(Save_Data.E_W, subString, subStringNext - subString);break;	//获取E/W
						case 7:memcpy(Save_Data.Speed, subString, subStringNext - subString);break;

						default:break;
					}

					subString = subStringNext;
					Save_Data.isParseData = true;
					if(usefullBuffer[0] == 'A')
						Save_Data.isUsefull = true;
					else if(usefullBuffer[0] == 'V')
						Save_Data.isUsefull = false;

				}
				else
				{
					;//errorLog(2);	//解析错误
				}
			}


		}
	}
}
void Delay5s(void)
{
	int i=0;
	for (i=0;i<3;i++)
		delay_ms(1000);
}


char ListWifi[]="AT+CWLAP\r\n";
char WifiMode[]="AT+CWMODE=3\r\n";
char WifiConnect[]="AT+CWJAP=\"SsaE\",\"123456789\"\r\n"; 
char WifiRst[]="AT+RST\r\n";
//char WifiMConnection[]="AT+CIPMUX=1\r\n";
char WifiTcp[]="AT+CIPSTART=\"TCP\",\"192.168.4.1\",5000\r\n";
char WifiTcpClose[]="AT+CIPCLOSE\r\n";
void Delay_5s(void)
{
	int i;
	for(i=0;i<10;i++)
	delay_ms(500);
}
void WifiInit(void)
{
	uart1SendString(WifiMode);
	delay_ms(1000);
	uart1SendString(WifiRst);
	Delay_5s();
	uart1SendString(WifiConnect);
	Delay_5s();
	Delay_5s();
	//uart1SendString(WifiMConnection);
	//delay_ms(200);
	
}
void WifiSendMsg(char* Msg)
{
	char AtSend[20];
	sprintf(AtSend,"AT+CIPSEND=%d\r\n",strlen(Msg));
	uart1SendString(WifiTcp);
	delay_ms(1000);
	uart1SendString(AtSend);
	delay_ms(500);
	uart1SendString(Msg);
	delay_ms(500);
	uart1SendString(WifiTcpClose);
	delay_ms(500);
}

char LoginMsg[]="{\"method\": \"update\",\"gatewayNo\": \"01\",\"userkey\": \"**************\"}&^!";
char DataMsg[300];
char NumberPeople[30];
int SearchWifi=0;
int TcpConnect=0;

int main(void)
{ 
	u8 res=1;
	int TimeCount=0;
	char GPS_DIS_latitude[30];
	char GPS_DIS_longitude[30];
	char GPS_DIS_speed[30];
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	delay_init(168);		
	usart1_init(115200);
	uart_init(9600);
	usart3_init(115200);
	TIM7_Int_Init(1000-1,8400-1);		//100ms中断
	ExitInit();
	TIM_Cmd(TIM7, DISABLE); //关闭定时器7
	LED_Init();		  	
	POINT_COLOR=RED;
	BACK_COLOR=WHITE;
	LCD_Init();
	LCD_Clear(WHITE);
	clrStruct();
	XFS_FrameInfo("欢迎使用智能公交车系统!");
	/*
	WifiInit();
	while(1)
	{
		WifiSendMsg("S15E\r\n");
		delay_ms(500);
		LED0=!LED0;
	}*/
	

	
	LCD_ShowString(30,0,(u8*)"3G init......",POINT_COLOR);
	XFS_FrameInfo("3G模块初始化中");
	PFout(0)=1;
	delay_ms(2000);
	PFout(0)=0;
	while(res)
	{
		res=GSM_Dect();
		delay_ms(2000);
	}	
	LCD_Clear(WHITE);
	XFS_FrameInfo("3G模块初始化成功!");
	res = 1;
	sim900a_send_cmd((u8*)"ATH",(u8*)"OK",2);
	LCD_ShowString(30,0,(u8*)"Connecting...",POINT_COLOR);
	
		while(res)
	{
		res=SIM900A_CONNECT_SERVER_SEND_INFOR((u8*)"112.74.102.42",(u8*)"8001");
		delay_ms(200);
		
	}
	XFS_FrameInfo("欢迎使用智能公交车系统!");
	
	delay_ms(600);
	LCD_Clear(WHITE);
	
	res=1;
	SIM900A_GPRS_SEND_DATA((u8*)"tem");
	delay_ms(600);
	SIM900A_GPRS_SEND_DATA((u8*)"tem");
	delay_ms(600);
	LCD_ShowString(30,0,(u8*)"Connecting To Server!",POINT_COLOR);
	
	
	
	
	while(1)
	{
		parseGpsBuffer();
		if(Save_Data.isUsefull)
		{
			LED0=!LED0;
			TimeCount++;
			if(TimeCount>300)
				TimeCount=0;
			memset(GPS_DIS_latitude,0,sizeof(GPS_DIS_latitude));
			memset(GPS_DIS_longitude,0,sizeof(GPS_DIS_longitude));
			memset(GPS_DIS_speed,0,sizeof(GPS_DIS_speed));
			sprintf(GPS_DIS_latitude,"Gps_latitude:%s",Save_Data.latitude);
			sprintf(GPS_DIS_longitude,"Gps_longitude:%s",Save_Data.longitude);
			sprintf(GPS_DIS_speed,"Gps_speed:%skm/h",Save_Data.Speed);
			
			if(TimeCount%20==0)
			{
				//sim900a_send_cmd((u8*)"AT+CIPCLOSE",(u8*)"OK",20);
				LCD_ShowString(30,100,(u8*)"3G:Msg Uploading",POINT_COLOR);
				
				res=1;
				while(res)
				{
					res=SIM900A_CONNECT_SERVER_SEND_INFOR((u8*)"101.37.32.173",(u8*)"9960");
					delay_ms(200);
					
				}
				
				SIM900A_GPRS_SEND_DATA((u8*)LoginMsg);
				memset(DataMsg,0,sizeof(DataMsg));
				sprintf(DataMsg,"{\"method\": \"upload\",\"data\":[{\"Name\":\"G1\",\"Value\":\"%s\"},{\"Name\":\"G2\",\"Value\":\"%s\"},{\"Name\":\"G3\",\"Value\":\"%s\"},{\"Name\":\"G4\",\"Value\":\"%d\"}]}&^!",Save_Data.latitude,Save_Data.longitude,Save_Data.Speed,People);
				SIM900A_GPRS_SEND_DATA((u8*)DataMsg);
				sim900a_send_cmd((u8*)"AT+CIPCLOSE",(u8*)"OK",20);
				XFS_FrameInfo("上传位置信息完成");
				LCD_ShowString(30,100,(u8*)"                  ",POINT_COLOR);
			}
			else if(TimeCount%50==0)
			{
				LCD_ShowString(30,100,(u8*)"3G:Get Temperature",POINT_COLOR);
				res=1;
					while(res)
					{
						res=SIM900A_CONNECT_SERVER_SEND_INFOR((u8*)"112.74.102.42",(u8*)"8001");
						delay_ms(200);
		
					}
					SIM900A_GPRS_SEND_DATA((u8*)"tem");
					delay_ms(600);
					LCD_ShowString(30,100,(u8*)"                  ",POINT_COLOR);
					XFS_FrameInfo("温度信息已更新");
				
			}
			
				if((TimeCount%20==0)&&(!SearchWifi))
				uart1SendString(ListWifi);
				if(SearchWifi&&(!TcpConnect))
				{
					LCD_ShowString(30,60,(u8*)"Wifi Connecting",POINT_COLOR);
					LED1=!LED1;
					WifiInit();
					TcpConnect=1;
				}
				if(TcpConnect)
				{
					XFS_FrameInfo("到达黑龙江大学站");
					WifiSendMsg("S15E");
					LCD_ShowString(30,60,(u8*)"               ",POINT_COLOR);
					LCD_ShowString(30,60,(u8*)"Wifi:Msg Uploading",POINT_COLOR);
					delay_ms(500);
					TcpConnect=0;
					SearchWifi=0;
					LCD_ShowString(30,60,(u8*)"                  ",POINT_COLOR);
				}
			LCD_ShowString(30,0,(u8*)GPS_DIS_latitude,POINT_COLOR);
			LCD_ShowString(30,20,(u8*)GPS_DIS_longitude,POINT_COLOR);
			LCD_ShowString(30,40,(u8*)GPS_DIS_speed,POINT_COLOR);
			LCD_ShowString(30,80,(u8*)TemMsg,POINT_COLOR);
			
			
					
			
		}
		else
		{
			LCD_ShowString(30,0,(u8*)"No gps signal!",POINT_COLOR);
			LED0=0;
		}
		

	memset(NumberPeople,0,sizeof(NumberPeople));
		sprintf(NumberPeople,"Number of People:%d",People);
		LCD_ShowString(30,120,"                     ",POINT_COLOR);
		LCD_ShowString(30,120,(u8*)NumberPeople,POINT_COLOR);
	//SIM900A_GPRS_SEND_DATA((u8*)"unv-sim-tcp-ip-test");
		delay_ms(300);
	}
}

